package com.atlin.meta;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.atlin.meta.enums.FileGenerateTypeEnum;
import com.atlin.meta.enums.FileTypeEnum;
import com.atlin.meta.enums.ModelTypeEnum;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author atlin
 * @description 元信息校验
 * @createDate 2024/9/28 14:22
 */
public class MetaValidator {

    public static void doValidateAndFill(Meta meta) {
        //1. 基本信息校验
        validAndFillMetaRoot(meta);

        //2. 校验文件配置
        validAndFillMetaFileConfig(meta);

        //3.模型数据配置
        validAndFillModelConfig(meta);
    }

    private static void validAndFillModelConfig(Meta meta) {
        Meta.ModelConfig modelConfig = meta.getModelConfig();
        if (modelConfig == null) {
            return;
        }
        List<Meta.ModelConfig.ModelInfo> modelInfoList = modelConfig.getModels();
        if (CollectionUtil.isEmpty(modelInfoList)) {
            return;
        }
        for (Meta.ModelConfig.ModelInfo modelInfo : modelInfoList) {
            String fieldName = modelInfo.getFieldName();
            if (StrUtil.isBlank(fieldName)) {
                throw new MetaException("未填写：fieldName");
            }
            String modelInfoType = modelInfo.getType();
            if (StrUtil.isBlank(modelInfoType)) {
                modelInfo.setType(ModelTypeEnum.STRING.getValue());
            }
        }
    }

    private static void validAndFillMetaFileConfig(Meta meta) {
        if (meta == null) return;
        Meta.FileConfig fileConfig = meta.getFileConfig();
        if (fileConfig == null) {
            throw new MetaException("未填写：fileConfig");
        }
        String sourceRootPath = fileConfig.getSourceRootPath();
        
        // sourceRootPath：必填
        if (StrUtil.isBlank(sourceRootPath)) {
            throw new MetaException("未填写：sourceRootPath");
        }
        
        // outputRootPath：默认为当前路径下的 generated
        String outputRootPath = fileConfig.getOutputRootPath();
        String defaultOutputRootPath = "generated";
        if (StrUtil.isBlank(outputRootPath)) {
            fileConfig.setOutputRootPath(defaultOutputRootPath);
        }
        
        // inputRootPath：.source + sourceRootPath 的最后一个层级路径
        String inputRootPath = fileConfig.getInputRootPath();
        String defaultInputRootPath = ".source" + File.separator + FileUtil.getLastPathEle(Paths.get(sourceRootPath));
        if (StrUtil.isBlank(inputRootPath)) {
            fileConfig.setOutputRootPath(defaultInputRootPath);
        }
        
        String fileConfigType = fileConfig.getType();
        String defaultType = FileTypeEnum.DIR.getValue();
        if (StrUtil.isBlank(fileConfigType)) {
            fileConfig.setType(defaultType);
        }

        // fileInfo 默认值
        List<Meta.FileConfig.FileInfo> fileInfoList = fileConfig.getFiles();
        if (CollectionUtil.isEmpty(fileInfoList)) {
            return;
        }
        for (Meta.FileConfig.FileInfo fileInfo : fileInfoList) {
            // inputPath: 必填
            String inputPath = fileInfo.getInputPath();
            if (StrUtil.isBlank(inputPath)) {
                throw new MetaException("未填写 inputPath");
            }

            // outputPath: 默认等于 inputPath
            String outputPath = fileInfo.getOutputPath();
            if (StrUtil.isEmpty(outputPath)) {
                fileInfo.setOutputPath(inputPath);
            }
            // type：默认 inputPath 有文件后缀（如 .java）为 file，否则为 dir
            String type = fileInfo.getType();
            if (StrUtil.isBlank(type)) {
                // 无文件后缀
                if (StrUtil.isBlank(FileUtil.getSuffix(inputPath))) {
                    fileInfo.setType(FileTypeEnum.DIR.getValue());
                } else {
                    fileInfo.setType(FileTypeEnum.FILE.getValue());
                }
            }
            // generateType：如果文件结尾不为 Ftl，generateType 默认为 static，否则为 dynamic
            String generateType = fileInfo.getGenerateType();
            if (StrUtil.isBlank(generateType)) {
                // 为动态模板
                if (inputPath.endsWith(".ftl")) {
                    fileInfo.setGenerateType(FileGenerateTypeEnum.DYNAMIC.getValue());
                } else {
                    fileInfo.setGenerateType(FileGenerateTypeEnum.STATIC.getValue());
                }
            }
        }
    }

    private static void validAndFillMetaRoot(Meta meta) {
        String name = meta.getName();
        String description = meta.getDescription();
        String basePackage = meta.getBasePackage();
        String version = meta.getVersion();
        String author = meta.getAuthor();
        String createTime = meta.getCreateTime();

        if (StrUtil.isBlank(name)) {
            name = "my-generator";
            meta.setName(name);
        }
        if (StrUtil.isEmpty(description)) {
            description = "我的模版生成器";
            meta.setName(description);
        }
        if (StrUtil.isBlank(basePackage)) {
            basePackage = "com.atlin";
            meta.setName(basePackage);
        }
        if (StrUtil.isEmpty(version)) {
            version = "1.0";
            meta.setName(version);
        }
        if (StrUtil.isEmpty(author)) {
            author = "atlin";
            meta.setName(author);
        }
        if (StrUtil.isEmpty(createTime)) {
            createTime = DateUtil.now();
            meta.setName(createTime);
        }
    }

}
